package com.biz.primus.ms.base.vo;

import com.biz.primus.base.exception.BizParameterException;
import com.biz.primus.common.vo.SpringPageVO;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import org.springframework.data.domain.Page;

/**
 * Created by defei on 9/19/17.
 */
public class SpringPageToSpringPageVO {

    public static <T,S> SpringPageVO<T> of(Page<S> page, Function<S, T> applyFunction){

        SpringPageVO<T> springPageVO = new SpringPageVO<>();
        if(page != null){
            if(applyFunction == null){
                throw new BizParameterException("Argument applyFunction is null");
            }
            springPageVO.setContent(Lists.transform(page.getContent(), applyFunction::apply));
            springPageVO.setNumber(page.getNumber());
            springPageVO.setSize(page.getSize());
            springPageVO.setTotalElements(page.getTotalElements());
        }
        return springPageVO;
    }
}
